/**
 * 
 */
package com.taobao.api.direct;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import com.taobao.api.ApiConstants;
import com.taobao.api.TaobaoApiException;
import com.taobao.api.TaobaoApiMethod;
import com.taobao.api.convert.TaobaoAppJSONConvert;
import com.taobao.api.convert.TaobaoItemJSONConvert;
import com.taobao.api.convert.TaobaoProductJSONConvert;
import com.taobao.api.convert.TaobaoSellerJSONConvert;
import com.taobao.api.convert.TaobaoShopJSONConvert;
import com.taobao.api.convert.TaobaoTradeJSONConvert;
import com.taobao.api.convert.TaobaoUserJSONConvert;
import com.taobao.api.extra.TaobaoExtraJsonRestClient;
import com.taobao.api.extra.model.CatStatsGetRequest;
import com.taobao.api.extra.model.CatStatsGetResponse;
import com.taobao.api.extra.model.ItemDeleteRequest;
import com.taobao.api.extra.model.ItemDeleteResponse;
import com.taobao.api.extra.model.ItemStatsGetRequest;
import com.taobao.api.extra.model.ItemStatsGetResponse;
import com.taobao.api.extra.model.ItemsDownloadRequest;
import com.taobao.api.extra.model.ItemsDownloadResponse;
import com.taobao.api.extra.model.ShippingAddRequest;
import com.taobao.api.extra.model.ShippingAddResponse;
import com.taobao.api.extra.model.SkusOutGetRequest;
import com.taobao.api.extra.model.SkusOutGetResponse;
import com.taobao.api.extra.model.TradeAddRequest;
import com.taobao.api.extra.model.TradeAddResponse;
import com.taobao.api.extra.model.TradeStatsGetRequest;
import com.taobao.api.extra.model.TradeStatsGetResponse;
import com.taobao.api.json.JSONArray;
import com.taobao.api.json.JSONException;
import com.taobao.api.json.JSONObject;
import com.taobao.api.model.*;
import com.taobao.api.util.DateUtil;

/**
 * 指定登录客户端JSON版，构造时需要制定合作伙伴ID
 * 
 * @version 2008-11-17
 * @author <a href="mailto:zixue@taobao.com">zixue</a>
 */
public class TaobaoDirectJsonRestClient extends TaobaoExtraJsonRestClient
		implements TaobaoDirectRestClient {
	private static final String SERVICE_URL = "http://api.taobao.com/private/services/rest";
	private static final String SESSION_NICK = "session_nick";
	private static final String PARTNER_ID = "partner_id";

	/**
	 * 合作伙伴id
	 */
	private String partnerId;

	public TaobaoDirectJsonRestClient(String partnerId, String appkey,
			String secret) throws TaobaoApiException {
		this(partnerId, SERVICE_URL, appkey, secret);
	}

	public TaobaoDirectJsonRestClient(String partnerId, String serviceUrl,
			String appkey, String secret) throws TaobaoApiException {
		super(serviceUrl, appkey, secret);
		this.partnerId = partnerId;
	}

	public TaobaoDirectJsonRestClient(String partnerId, String serviceUrl,
			String version, String appkey, String secret)
			throws TaobaoApiException {
		super(serviceUrl, version, appkey, secret);
		this.partnerId = partnerId;
	}

	public ItemCatsResponse itemCatsGet(ItemCatsGetRequest itemCatsGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemCatsGet(itemCatsGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemCatsGet(params, sessionId);
	}

	public TaobaoDirectJsonRestClient(String appkey, String secret,
			boolean isSandbox) throws TaobaoApiException {
		super(appkey, secret, isSandbox);
	}

	public TaobaoDirectJsonRestClient(String appkey, String secret)
			throws TaobaoApiException {
		super(appkey, secret);
	}

	public ItemGetResponse itemGet(ItemGetRequest itemGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemGet(itemGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemGet(params, sessionId);
	}

	public ShopGetResponse shopGet(ShopGetRequest shopGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareShopGet(shopGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendShopGet(params, sessionId);
	}

	public ShopUpdateResponse shopUpdate(ShopUpdateRequest shopUpdateRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareShopUpdate(shopUpdateRequest);
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SHOP_UPDATE.getMethod(),
						sessionId, params));
		ShopUpdateResponse shopUpdateResponse = new ShopUpdateResponse(response);
		// 平台成功访问
		if (shopUpdateResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(shopUpdateResponse.getBody());
				parseError(shopUpdateResponse, json);
				// 访问淘宝成功
				if (shopUpdateResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHOPS)) {
						JSONArray shops = rsp.getJSONArray(ApiConstants.SHOPS);
						JSONObject shop = shops.getJSONObject(0);
						shopUpdateResponse.setShop(TaobaoShopJSONConvert
								.convertJsonToShop(shop));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shopUpdateResponse;
	}

	public ShopShowcaseRemainCountResponse shopShowcaseRemianCount(
			ShopShowcaseRemainCountRequest showcaseRemainCountRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SHOP_SHOWCASE_REMAINCOUNT
						.getMethod(), sessionId, params));
		ShopShowcaseRemainCountResponse shopShowcaseRemainCountResponse = new ShopShowcaseRemainCountResponse(
				response);
		// 平台成功访问
		if (shopShowcaseRemainCountResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(
						shopShowcaseRemainCountResponse.getBody());
				parseError(shopShowcaseRemainCountResponse, json);
				// 访问淘宝成功
				if (shopShowcaseRemainCountResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHOPS)) {
						JSONArray shops = rsp.getJSONArray(ApiConstants.SHOPS);
						JSONObject shop = shops.getJSONObject(0);
						try {
							shopShowcaseRemainCountResponse
									.setRemainShowcase(Integer
											.parseInt(shop
													.getString(ApiConstants.REMAIN_COUNT)));
						} catch (JSONException e) {
							throw new TaobaoApiException(e);
						}
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shopShowcaseRemainCountResponse;
	}

	public SellerCatsListGetResponse sellerCatsListGet(
			SellerCatsListGetRequest request, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareSellerCatsListGet(request);
		params.put(SESSION_NICK, request.getNick());
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SELLERCATS_LIST_GET
						.getMethod(), params));
		SellerCatsListGetResponse sellerCatsListGetResponse = new SellerCatsListGetResponse(
				response);
		// 平台访问成功
		if (sellerCatsListGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(sellerCatsListGetResponse.getBody());
				parseError(sellerCatsListGetResponse, json);
				// 淘宝访问成功
				if (sellerCatsListGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SELLER_CATS)) {
						JSONArray sellercats = rsp
								.getJSONArray(ApiConstants.SELLER_CATS);
						sellerCatsListGetResponse
								.setSellerCats(TaobaoSellerJSONConvert
										.convertJsonArrayToSellerCatList(sellercats));
					}
					setTotalResults(rsp, sellerCatsListGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return sellerCatsListGetResponse;
	}

	
	public SellerCatsListAddResponse sellerCatsListAdd(
			SellerCatsListAddRequest request, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareSellerCatsListAdd(request);
		params.put(SESSION_NICK, sessionNick);
		return sendSellerCatsListAdd(params, request, sessionId);
	}
	
	public SellerCatsListUpdateResponse sellerCatsListUpdate(SellerCatsListUpdateRequest request,String sessionId,String sessionNick) throws TaobaoApiException{
		Map<String, Object> params = prepareSellerCatsListUpdate(request);
		params.put(SESSION_NICK, sessionNick);
		return sendSellerCatsListUpdate(params, request, sessionId);
	}
	
	/* 
	 *
	 * (non-Javadoc)
	 * 
	 * @see com.taobao.api.direct.TaobaoDirectApi#userGet(com.taobao.api.model.UserGetRequest,
	 *      java.lang.String, java.lang.String)
	 */
	public UserGetResponse userGet(UserGetRequest userGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareUserGet(userGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendUserGet(params, sessionId);
	}
	
	public UserDetailGetResponse userDetail(
			UserDetailGetRequest userDetailGetRequest, String sessionId,String sessionNick)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.FIELDS, userDetailGetRequest.getFields());
		params.put(ApiConstants.NICK, userDetailGetRequest.getNick());
		params.put(SESSION_NICK, sessionNick);
		return sendUserDetailGet(params, sessionId);
	}
	
	public UsersGetResponse usersGet(UsersGetRequest usersGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将usersGetRequest请求中的参数添加到params
		params.put(ApiConstants.FIELDS, usersGetRequest.getFields());
		params.put(ApiConstants.NICKS, usersGetRequest.getNicks());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.USERS_GET.getMethod(),
						params));
		UsersGetResponse usersGetResponse = new UsersGetResponse(response);
		// 平台访问成功
		if (usersGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(usersGetResponse.getBody());
				parseError(usersGetResponse, json);
				// 淘宝访问成功
				if (usersGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.USERS)) {
						JSONArray users = rsp.getJSONArray(ApiConstants.USERS);
						usersGetResponse.setUsers(TaobaoUserJSONConvert
								.convertJsonArrayToUserList(users));
					}
					setTotalResults(rsp, usersGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return usersGetResponse;
	}

	public ItemUpdateResponse itemUpdate(ItemUpdateRequest itemUpdateRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemUpdate(itemUpdateRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemUpdate(itemUpdateRequest, params, sessionId);
	}
	
	public ItemUpdateResponse itemTbsUpdate(ItemTbsUpdateRequest request,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.IID, request.getIid());
		params.put(ApiConstants.IS_TAOBAO, request.getIsTaobao());
		params.put(ApiConstants.IS_EX, request.getIsEx());
		
		params.put(SESSION_NICK, sessionNick);
		
		TaobaoResponse response = this.getFetch().fetch(this.getServiceUrl(),
				getTemplateRequest(TaobaoDirectApiMethod.ITEM_TBS_UPDATE.getMethod(),
						sessionId, params));
		
		ItemUpdateResponse itemUpdateResponse = new ItemUpdateResponse(response);
		// 平台成功访问
		if (itemUpdateResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(itemUpdateResponse.getBody());
				parseError(itemUpdateResponse, json);
				// 访问淘宝成功
				if (itemUpdateResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						Item updatedItem = new Item();
						try {
							updatedItem.setModified(DateUtil.strToDate(item.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						updatedItem.setIid(item.getString(ApiConstants.IID));
						itemUpdateResponse.setItem(updatedItem);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemUpdateResponse;
	}

	/*
	 * 增加session_nick
	 * 
	 * @seecom.taobao.api.direct.TaobaoDirectApi#itemAdd(com.taobao.api.model.
	 * ItemAddRequest, java.lang.String, java.lang.String)
	 */
	public ItemAddResponse itemAdd(ItemAddRequest itemAddRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemAdd(itemAddRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemAdd(params, itemAddRequest, sessionId);
	}

	public ItemImgUploadResponse itemImgUpload(
			ItemImgUploadRequest itemImgUploadRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemImgUpload(itemImgUploadRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemImgUpload(itemImgUploadRequest, params, sessionId);
	}

	public PropimgUploadResponse propImgUpload(
			PropimgUploadRequest propimgUploadRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = preparePropImgUpload(propimgUploadRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendPropImgUpload(propimgUploadRequest, params, sessionId);
	}

	public ItemImgDeleteResponse itemImgDelete(
			ItemImgDeleteRequest itemImgDeleteRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemImgDelete(itemImgDeleteRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemImgDelete(params, sessionNick);
	}
    public ItemDeleteResponse  itemDelete(ItemDeleteRequest itemDeleteRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put(SESSION_NICK, sessionNick);
		params.put(ApiConstants.IID, itemDeleteRequest.getIid());
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoDirectApiMethod.ITEM_DELETE.getMethod(),
						sessionId, params));
		ItemDeleteResponse itemDeleteResponse = new ItemDeleteResponse(
				response);
		if (itemDeleteResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemDeleteResponse.getBody());
				parseError(itemDeleteResponse, json);
				if (itemDeleteResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray itemImgs = rsp
								.getJSONArray(ApiConstants.ITEMS);
						JSONObject item= itemImgs.getJSONObject(0);
						Item deleteItem = new Item();
						deleteItem.setIid(item
								.getString(ApiConstants.IID));
						try {
							deleteItem.setCreated(DateUtil.strToDate(item
									.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							
						}
						itemDeleteResponse.setItem(deleteItem);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemDeleteResponse;
    }
	public PropImgDeleteResponse propImgDelete(
			PropImgDeleteRequest propImgDeleteRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = preparePropImgDelete(propImgDeleteRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendPropImgDelete(params, sessionNick);
	}

	public SkuAddResponse skuAdd(SkuAddRequest skuAddRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareSkuAdd(skuAddRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendSkuAdd(params, sessionId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.taobao.api.direct.TaobaoDirectApi#tradeGet(com.taobao.api.model.TradeGetRequest,
	 *      java.lang.String, java.lang.String) =======
	 *  /* (non-Javadoc)
	 * 
	 * @see com.taobao.api.direct.TaobaoDirectApi#tradeGet(com.taobao.api.model.TradeGetRequest,
	 *      java.lang.String, java.lang.String) >>>>>>> .r72
	 */
	public TradeGetResponse tradeGet(TradeGetRequest tradeGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeGet(tradeGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradeGet(params, sessionId);
	}
	
	protected TradeGetResponse sendTradeFullInfoGet(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		TaobaoResponse response = this
				.getFetch()
				.fetch(
						this.getServiceUrl(),
						getTemplateRequest(
								com.taobao.api.direct.TaobaoDirectApiMethod.TRADE_FULLINFO_GET
										.getMethod(), sessionId, params));
		TradeGetResponse tradeGetResponse = new TradeGetResponse(response);
		// 平台成功访问
		if (tradeGetResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(tradeGetResponse.getBody());
				parseError(tradeGetResponse, json);
				// 访问淘宝成功
				if (tradeGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADES)) {
						JSONArray trades = rsp
								.getJSONArray(ApiConstants.TRADES);
						JSONObject trade = trades.getJSONObject(0);
						tradeGetResponse.setTrade(TaobaoTradeJSONConvert
								.convertJsonToTrade(trade));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradeGetResponse;
	}
	public TradeGetResponse tradeFullInfoGet(TradeGetRequest tradeGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeGet(tradeGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradeFullInfoGet(params, sessionId);
	}
	
	public TradeStatsGetResponse tradeStatsGet(
			TradeStatsGetRequest tradeStatRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeStat(tradeStatRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradeStat(params, sessionId);
	}

	public CatStatsGetResponse catStatsGet(CatStatsGetRequest catStatRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareCatStat(catStatRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendCatStat(params, sessionId);
	}

	public ItemStatsGetResponse itemStatsGet(
			ItemStatsGetRequest itemStatRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemStat(itemStatRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemStat(params, sessionId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.taobao.api.direct.TaobaoDirectApi#itemGet(com.taobao.api.model.
	 * ItemGetRequest, java.lang.String, java.lang.String)
	 */
	public ItemsGetResponse itemsOnSaleGet(
			ItemsOnSaleGetRequest itemsOnSaleGetRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemsOnSaleGet(itemsOnSaleGetRequest);
		params.put(SESSION_NICK, sessionNick);
		params.put(ApiConstants.ITEM_TYPE, itemsOnSaleGetRequest.getItemType());
		return sendItemsOnSaleGet(params, sessionId);
	}
    
	public ItemsGetResponse itemsAllGet(ItemsAllGetRequest itemsAllGetRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemsAllGet(itemsAllGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemsAllGet(params, sessionId);
	}

	public ItemsGetResponse itemsInStockGet(ItemsInStockGetRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		return this.itemsInventoryGet(req, sessionId, sessionNick);
	}

	public ItemsGetResponse itemsInventoryGet(ItemsInventoryGetRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemsInStockGet(req);
		params.put(SESSION_NICK, sessionNick);
		params.put(ApiConstants.ITEM_TYPE, req.getItemType());
		return getItemsInventory(params, sessionId);
	}

	public ItemsDownloadResponse itemsDownload(
			ItemsDownloadRequest itemsDownloadRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemsDownload(itemsDownloadRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemsDownload(params, sessionId);
	}

	/**
	 * 下载一定条件的商品
	 * 
	 * @param itemsDownloadRequest
	 * @return ItemsDownloadResponse
	 * @throws TaobaoApiException
	 */
	public ItemsDownloadResponse itemsDownload(
			ItemsDownloadRequest itemsDownloadRequest, String sessionId)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemsDownload(itemsDownloadRequest);
		return sendItemsDownload(params, sessionId);
	}

	protected Map<String, Object> prepareItemsDownload(
			ItemsDownloadRequest itemsDownloadRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.SELLER_CIDS, itemsDownloadRequest
				.getSellerCids());
		params.put(ApiConstants.CID, itemsDownloadRequest.getCid());
		params.put(ApiConstants.Q, itemsDownloadRequest.getQ());
		params.put(ApiConstants.APPROVE_STATUS, itemsDownloadRequest
				.getApproveStatus());
		params
				.put(ApiConstants.START_DATE, itemsDownloadRequest
						.getStartDate());
		params.put(ApiConstants.END_DATE, itemsDownloadRequest.getEndDate());
		params.put(ApiConstants.PAGE_NO, itemsDownloadRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, itemsDownloadRequest.getPageSize());
		return params;
	}

	protected ItemsDownloadResponse sendItemsDownload(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_DOWNLOAD.getMethod(),
						sessionId, params));
		ItemsDownloadResponse itemsDownloadResponse = new ItemsDownloadResponse(
				response);
		if (itemsDownloadResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemsDownloadResponse.getBody());
				parseError(itemsDownloadResponse, json);
				if (itemsDownloadResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						itemsDownloadResponse.setItems(TaobaoItemJSONConvert
								.convertJsonArrayToItemsList(items));
					}
					setTotalResults(rsp, itemsDownloadResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemsDownloadResponse;
	}

	public TradesGetResponse tradesBoughtGet(
			TradesBoughtGetRequest tradesRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradesBought(tradesRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradesGet(params, sessionId, TaobaoApiMethod.TRADES_BOUGHT_GET);
	}

	public TradesGetResponse tradesSoldGet(TradesSoldGetRequest tradesRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradesSold(tradesRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradesGet(params, sessionId, TaobaoApiMethod.TRADES_SOLD_GET);
	}
	
	public TradesGetResponse tradesSoldIncrementGet(TradesSoldIncrementGetRequest req,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradesSoldV2(req);
		params.put(SESSION_NICK, sessionNick);
		return sendTradesGet(params, sessionId, TaobaoApiMethod.TRADES_SOLD_INCREMENT_GET);
	}

	public ProductAddResponse productAdd(ProductAddRequest productAddRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareProductAdd(productAddRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendProductAdd(params, productAddRequest, sessionId);
	}
	

	public ProductUpdateResponse productUpdate(
			ProductUpdateRequest productUpdateRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareProductUpdate(productUpdateRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendProductUpdate(params, productUpdateRequest, sessionId);
	}


	public ProductImgUploadResponse productImgUpload(
			ProductImgUploadRequest productImgUploadRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ProductImgUploadRequest.PICID, productImgUploadRequest
				.getPicId());
		params.put(ProductImgUploadRequest.PRODUCTID, productImgUploadRequest
				.getProductId());
		params.put(ProductImgUploadRequest.MAJOR, productImgUploadRequest
				.getMajor());
		params.put(ProductImgUploadRequest.POSITION, productImgUploadRequest
				.getPosition());
		params.put(SESSION_NICK, sessionNick);
		ProductImgUploadResponse productImgUploadResponse = getProductImgUploadResponse(
				productImgUploadRequest, sessionId, params);
		// 平台成功访问
		if (productImgUploadResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(productImgUploadResponse
						.getBody());
				parseError(productImgUploadResponse, json);
				// 访问淘宝成功
				if (productImgUploadResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ProductImgUploadRequest.PRODUCTIMG)) {
						JSONArray products = rsp
								.getJSONArray(ProductImgUploadRequest.PRODUCTIMG);
						JSONObject product = products.getJSONObject(0);
						ProductImg addedProduct = new ProductImg();
						try {
							addedProduct.setCreated(DateUtil.strToDate(product
									.getString(ApiConstants.CREATED)));
							addedProduct.setModified(DateUtil.strToDate(product
									.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						addedProduct.setPicId(product
								.getString(ApiConstants.PICID));
						addedProduct
								.setUrl(product.getString(ApiConstants.URL));
						productImgUploadResponse.setProductImg(addedProduct);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return productImgUploadResponse;
	}

	private ProductImgUploadResponse getProductImgUploadResponse(
			ProductImgUploadRequest productImgUploadRequest, String sessionId,
			Map<String, Object> paramsMap) throws TaobaoApiException {
		TaobaoResponse rsp = null;
		// 根据添加商品是否有图片选择不同的请求方法
		if (productImgUploadRequest.getImage() != null) {
			rsp = this.getFetch().fetchWithFile(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.PRODUCT_IMG_UPLOAD
							.getMethod(), sessionId, paramsMap),
					productImgUploadRequest.getImage());
		} else {
			rsp = this.getFetch().fetch(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.PRODUCT_IMG_UPLOAD
							.getMethod(), sessionId, paramsMap));
		}
		return new ProductImgUploadResponse(rsp);
	}

	public ProductPropImgUploadResponse productPropImgUpload(
			ProductPropImgUploadRequest productPropImgUploadRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ProductPropImgUploadRequest.PICID,
				productPropImgUploadRequest.getPicId());
		params.put(ProductPropImgUploadRequest.PRODUCTID,
				productPropImgUploadRequest.getProductId());
		params.put(ProductPropImgUploadRequest.PROPS,
				productPropImgUploadRequest.getProps());
		params.put(ProductPropImgUploadRequest.POSITION,
				productPropImgUploadRequest.getPosition());
		params.put(SESSION_NICK, sessionNick);
		ProductPropImgUploadResponse productPropImgUploadResponse = getProductPropImgUploadResponse(
				productPropImgUploadRequest, sessionId, params);
		// 平台成功访问
		if (productPropImgUploadResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(productPropImgUploadResponse
						.getBody());
				parseError(productPropImgUploadResponse, json);
				// 访问淘宝成功
				if (productPropImgUploadResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ProductPropImgUploadRequest.PRODUCTPROPIMG)) {
						JSONArray products = rsp
								.getJSONArray(ProductPropImgUploadRequest.PRODUCTPROPIMG);
						JSONObject product = products.getJSONObject(0);
						ProductPropImg addedProduct = new ProductPropImg();
						try {
							addedProduct.setCreated(DateUtil.strToDate(product
									.getString(ApiConstants.CREATED)));
							addedProduct.setModified(DateUtil.strToDate(product
									.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						addedProduct.setPicId(product
								.getString(ApiConstants.PICID));
						addedProduct
								.setUrl(product.getString(ApiConstants.URL));
						productPropImgUploadResponse
								.setProductPropImg(addedProduct);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return productPropImgUploadResponse;
	}

	private ProductPropImgUploadResponse getProductPropImgUploadResponse(
			ProductPropImgUploadRequest productPropImgUploadRequest,
			String sessionId, Map<String, Object> paramsMap)
			throws TaobaoApiException {
		TaobaoResponse rsp = null;
		// 根据添加商品是否有图片选择不同的请求方法
		if (productPropImgUploadRequest.getImage() != null) {
			rsp = this.getFetch().fetchWithFile(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.PRODUCT_PROPIMG_UPLOAD
							.getMethod(), sessionId, paramsMap),
					productPropImgUploadRequest.getImage());
		} else {
			rsp = this.getFetch().fetch(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.PRODUCT_PROPIMG_UPLOAD
							.getMethod(), sessionId, paramsMap));
		}
		return new ProductPropImgUploadResponse(rsp);
	}

	public ProductImgDeleteResponse productImgDelete(
			ProductImgDeleteRequest productImgDeleteRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ProductImgDeleteRequest.PICID, productImgDeleteRequest
				.getPicId());
		params.put(ProductImgDeleteRequest.PRODUCTID, productImgDeleteRequest
				.getProductId());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.PRODUCT_IMG_DELETE
						.getMethod(), sessionId, params));
		ProductImgDeleteResponse productImgDeleteResponse = new

		ProductImgDeleteResponse(response);
		// 平台访问成功
		if (productImgDeleteResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(productImgDeleteResponse.getBody());
				parseError(productImgDeleteResponse, json);
				// 访问淘宝成功
				if (productImgDeleteResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ProductImgUploadRequest.PRODUCTIMG)) {
						JSONArray products = rsp.getJSONArray

						(ProductImgUploadRequest.PRODUCTIMG);
						JSONObject product = products.getJSONObject

						(0);
						productImgDeleteResponse.setProductImg

						(TaobaoProductJSONConvert
								.convertJsonToProductImg(product));
					}

				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return productImgDeleteResponse;
	}

	public ProductPropImgDeleteResponse productPropImgDelete(
			ProductPropImgDeleteRequest productPropImgDeleteRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ProductPropImgDeleteRequest.PICID,
				productPropImgDeleteRequest.getPicId());
		params.put(ProductPropImgDeleteRequest.PRODUCTID,
				productPropImgDeleteRequest.getProductId());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.PRODUCT_PROPIMG_DELETE
						.getMethod(), sessionId, params));
		ProductPropImgDeleteResponse productPropImgDeleteResponse = new ProductPropImgDeleteResponse(
				response);
		// 平台访问成功
		if (productPropImgDeleteResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(productPropImgDeleteResponse.getBody());
				parseError(productPropImgDeleteResponse, json);
				// 访问淘宝成功
				if (productPropImgDeleteResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ProductPropImgUploadRequest.PRODUCTPROPIMG)) {
						JSONArray products = rsp
								.getJSONArray(ProductPropImgUploadRequest.PRODUCTPROPIMG);
						JSONObject product = products.getJSONObject(0);
						productPropImgDeleteResponse
								.setProductPropImg(TaobaoProductJSONConvert
										.convertJsonToProductPropImg(product));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return productPropImgDeleteResponse;
	}

	public ShippingsSendGetResponse shippingsSendGet(ShippingsSendGetRequest request,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareLogisticsOrderGet(request);
		params.put(SESSION_NICK, sessionNick);
		return new ShippingsSendGetResponse(getLogisticsOrders(params, sessionId));
	}

	public ShippingsSendFullInfoGetResponse shippingsSendFullInfoGet(
			ShippingsSendFullInfoGetRequest request, String sessionId, String sessionNick)
			throws TaobaoApiException {
		Map<String, Object> params = prepareLogisticsOrderGet(request);
		params.put(SESSION_NICK, sessionNick);
		return new ShippingsSendFullInfoGetResponse(getLogisticsOrdersDetail(params, sessionId));
	}

	public DeliverySendResponse deliverySend(DeliverySendRequest request, String sessionId,
			String SessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareDeliverySend(request);
		params.put(SESSION_NICK, SessionNick);
		return sendDeliverySend(params, sessionId);
	}
	
	public ShippingAddressesGetResponse shippingAddressesGet(
			ShippingAddressesGetRequest shippingAddressGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareShippingAddresses(shippingAddressGetRequest);
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(
						TaobaoApiMethod.SHIPPING_ADDRESSES.getMethod(),
						sessionId, params));
		ShippingAddressesGetResponse shippingAddressGetResponse = new ShippingAddressesGetResponse(
				response);
		if (shippingAddressGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(shippingAddressGetResponse.getBody());
				parseError(shippingAddressGetResponse, json);
				if (shippingAddressGetResponse.isSuccess()) {
					// 处理返回结果
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHIPPING_ADDRESS)) {
						JSONArray shippingAddressList = rsp
								.getJSONArray(ApiConstants.SHIPPING_ADDRESS);
						shippingAddressGetResponse
								.setShippingAddressList(TaobaoUserJSONConvert
										.convertJsonArrayToShippingAddressList(shippingAddressList));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shippingAddressGetResponse;
	}

	public ItemsOutGetResponse itemsOutGet(ItemsOutGetRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		return new ItemsOutGetResponse(this.itemsCustomGet(req, sessionId, sessionNick));
	}

	public ItemsCustomGetResponse itemsCustomGet(ItemsCustomGetRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemsCustomGet(req);
		params.put(SESSION_NICK, sessionNick);
		return getCustomItems(params, sessionId);
	}

	public SkusOutGetResponse skusOutGet(SkusOutGetRequest req, String sessionId, String sessionNick)
			throws TaobaoApiException {
		return new SkusOutGetResponse(skusCustomGet(req, sessionId, sessionNick));
	}

	/*
	 * 增加partner_id
	 */
	@Override
	protected Map<String, CharSequence> getTemplateRequest(String method,
			String session, Map<String, Object> paramsMap) {
		paramsMap.put(PARTNER_ID, this.partnerId);
		return super.getTemplateRequest(method, session, paramsMap);
	}

	public TradeRatesGetResponse tradeRatesGet(
			TradeRatesGetRequest tradeRatesGetRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.FIELDS, tradeRatesGetRequest.getFields());
		params.put(ApiConstants.ROLE, tradeRatesGetRequest.getRole());
		params.put(ApiConstants.RATE_TYPE, tradeRatesGetRequest.getRateType());
		params.put(ApiConstants.RESULT, tradeRatesGetRequest.getResult());
		params.put(ApiConstants.PAGE_NO, tradeRatesGetRequest.getPageNo());
		params.put(ApiConstants.PAGE_SIZE, tradeRatesGetRequest.getPageSize());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADERATES_GET.getMethod(),
						sessionId, params));
		TradeRatesGetResponse tradeRatesGetResponse = new TradeRatesGetResponse(
				response);
		// 平台访问成功
		if (tradeRatesGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(tradeRatesGetResponse.getBody());
				parseError(tradeRatesGetResponse, json);
				// 访问淘宝成功
				if (tradeRatesGetResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADERATES)) {
						JSONArray tradeRates = rsp
								.getJSONArray(ApiConstants.TRADERATES);
						tradeRatesGetResponse
								.setTradeRates(TaobaoTradeJSONConvert
										.convertJsonArrayToTradeRateList(tradeRates));
					}
					setTotalResults(rsp, tradeRatesGetResponse);
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradeRatesGetResponse;
	}
	
	public TradeConfirmFeeGetResponse tradeConfirmFeeGet(
			TradeConfirmFeeGetRequest req, String sessionId, String sessionNick)
			throws TaobaoApiException {
		Map<String, Object> params = prepareTradeConfirmFeeGet(req);
		params.put(SESSION_NICK, sessionNick);
		return sendTradeConfirmFeeGet(params, sessionId);
	}

	public TradeRateAddResponse tradeRateAdd(
			TradeRateAddRequest tradeRateAddRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradeRateAddRequest请求中的参数添加到params
		params.put(ApiConstants.TID, tradeRateAddRequest.getTid());
		params.put(ApiConstants.ORDER_ID, tradeRateAddRequest.getOrderId());
		params.put(ApiConstants.ROLE, tradeRateAddRequest.getRole());
		params.put(ApiConstants.ANONY, tradeRateAddRequest.getAnony());
		params.put(ApiConstants.CONTENT, tradeRateAddRequest.getContent());
		params.put(ApiConstants.RESULT, tradeRateAddRequest.getResult());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADERATE_ADD.getMethod(),
						sessionId, params));
		TradeRateAddResponse tradeRateAddResponse = new TradeRateAddResponse(
				response);
		// 平台成功访问
		if (tradeRateAddResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(tradeRateAddResponse.getBody());
				parseError(tradeRateAddResponse, json);
				// 访问淘宝成功
				if (tradeRateAddResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADERATES)) {
						JSONArray tradeRates = rsp
								.getJSONArray(ApiConstants.TRADERATES);
						JSONObject tradeRate = tradeRates.getJSONObject(0);
						try {
							tradeRateAddResponse.setCreated(DateUtil
									.strToDate(tradeRate
											.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						tradeRateAddResponse.setTid(tradeRate
								.getString(ApiConstants.TID));
						tradeRateAddResponse.setOrderId(tradeRate
								.getString(ApiConstants.ORDER_ID));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradeRateAddResponse;
	}

	public TradeRateListAddResponse tradeRateListAdd(
			TradeRateListAddRequest tradeRateListAddRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将tradeRateAddRequest请求中的参数添加到params
		params.put(ApiConstants.TID, tradeRateListAddRequest.getTid());
		params.put(ApiConstants.ROLE, tradeRateListAddRequest.getRole());
		params.put(ApiConstants.ANONY, tradeRateListAddRequest.getAnony());
		params.put(ApiConstants.CONTENT, tradeRateListAddRequest.getContent());
		params.put(ApiConstants.RESULT, tradeRateListAddRequest.getResult());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.TRADERATE_LIST_ADD
						.getMethod(), sessionId, params));
		TradeRateListAddResponse tradeRateListAddResponse = new TradeRateListAddResponse(
				response);
		// 平台成功访问
		if (tradeRateListAddResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(tradeRateListAddResponse
						.getBody());
				parseError(tradeRateListAddResponse, json);
				// 访问淘宝成功
				if (tradeRateListAddResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADERATES)) {
						JSONArray tradeRates = rsp
								.getJSONArray(ApiConstants.TRADERATES);
						JSONObject tradeRate = tradeRates.getJSONObject(0);
						try {
							tradeRateListAddResponse.setCreated(DateUtil
									.strToDate(tradeRate
											.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						tradeRateListAddResponse.setTid(tradeRate
								.getString(ApiConstants.TID));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradeRateListAddResponse;
	}

	public TradeMemoAddResponse tradeMemoAdd(
			TradeMemoAddRequest tradeMemoAddRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeMemoAdd(tradeMemoAddRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradeMemoAdd(params, sessionId);
	}

	public TradeMemoUpdateResponse tradeMemoUpdate(
			TradeMemoUpdateRequest tradeMemoUpdateRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeMemoUpdate(tradeMemoUpdateRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradeMemoUpdate(params, sessionId);
	}

	public SuitesGetResponse suitesGet(SuiteGetRequest suiteGetRequest, String sessionId, String sessionNick)  throws TaobaoApiException{
		Map<String, Object> params = prepareSuitesGet(suiteGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendSuitesGet(params, sessionId);
	}
	
	public RefundGetResponse refundGet(RefundGetRequest refundGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareRefundGet(refundGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendRefundGet(params, sessionId);
	}
	public RefundMessagesGetResponse refundMessagesGet(RefundMessagesGetRequest refundMessagesGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareRefundMessagesGet(refundMessagesGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendRefundMessagesGet(params, sessionId);
	}
	public RefundMessageAddResponse refundMessageAdd(RefundMessageAddRequest refundMessageAddRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareRefundMessageAdd(refundMessageAddRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendRefundMessageAdd(params, refundMessageAddRequest, sessionId);
	}
	public RefundsApplyGetResponse refundsApplyGet(
			RefundsApplyGetRequest refundsApplyGetRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareRefundsApplayGet(refundsApplyGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendRefundsApplayGet(params, sessionId);
	}

	public RefundsRecieveGetResponse refundsRecieveGet(
			RefundsRecieveGetRequest refundsRecieveGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareRefundsRecieveGet(refundsRecieveGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendRefundsRecieveGet(params, sessionId);
	}


	
    public ItemExtraAddResponse  itemExtraAdd(ItemExtraAddRequest request,String sessionId,String sessionNick)throws TaobaoApiException{
    	Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.TITLE, request.getTitle());
		params.put(ApiConstants.TYPE, request.getType());
		params.put(ApiConstants.IID, request.getIid());
		params.put(ApiConstants.DESC, request.getDesc());
		params.put(ApiConstants.MEMO, request.getMemo());
		params.put(ApiConstants.FEATURE, request.getFeature());
		params.put(ApiConstants.RESERVE_PRICE, request.getReservePrice());
		params.put(ApiConstants.SKU_PRICES, request.getSkuPrices());
		params.put(ApiConstants.SKU_PROPERTIES, request.getSkuProperties());
		params.put(ApiConstants.SKU_QUANTITIES, request.getSkuQuantities());
		params.put(ApiConstants.SKU_MEMOS, request.getSkuMemos());
		params.put(ApiConstants.SKU_IDS, request.getSkuIds());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMEXTRA_ADD.getMethod(),
						sessionId, params));
		ItemExtraAddResponse itemExtraAddResponse = new ItemExtraAddResponse(response);
		// 平台成功访问
		if (itemExtraAddResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemExtraAddResponse.getBody());
				parseError(itemExtraAddResponse, json);
				// 访问淘宝成功
				if (itemExtraAddResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMEXTRAS)) {
						JSONArray itemextras = rsp.getJSONArray(ApiConstants.ITEMEXTRAS);
						JSONObject itemextra = itemextras.getJSONObject(0);
						ItemExtra updatedItem = new ItemExtra();
						try {
							updatedItem.setCreated(DateUtil.strToDate(itemextra
									.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						updatedItem.setEid(itemextra.getString(ApiConstants.EID));
						itemExtraAddResponse.setItemextra(updatedItem);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemExtraAddResponse;
    }
    public ItemExtraUpdateResponse  itemExtraUpdate(ItemExtraUpdateRequest request,String sessionId,String sessionNick)throws TaobaoApiException{
    	Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.TITLE, request.getTitle());
		params.put(ApiConstants.TYPE, request.getType());
		params.put(ApiConstants.IID, request.getIid());
		params.put(ApiConstants.DESC, request.getDesc());
		params.put(ApiConstants.MEMO, request.getMemo());
		params.put(ApiConstants.FEATURE, request.getFeature());
		params.put(ApiConstants.SKU_PRICES, request.getSkuPrices());
		params.put(ApiConstants.SKU_PROPERTIES, request.getSkuProperties());
		params.put(ApiConstants.SKU_QUANTITIES, request.getSkuQuantities());
		params.put(ApiConstants.RESERVE_PRICE, request.getReservePrice());
		params.put(ApiConstants.SKU_MEMOS, request.getSkuMemos());
		params.put(ApiConstants.SKU_IDS, request.getSkuIds());
		params.put(ApiConstants.SKU_EXTRA_IDS, request.getSkuExtraId());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMEXTRA_UPDATE.getMethod(),
						sessionId, params));
		ItemExtraUpdateResponse itemExtraUpdateResponse = new ItemExtraUpdateResponse(response);
		// 平台成功访问
		if (itemExtraUpdateResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemExtraUpdateResponse.getBody());
				parseError(itemExtraUpdateResponse, json);
				// 访问淘宝成功
				if (itemExtraUpdateResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMEXTRAS)) {
						JSONArray itemextras = rsp.getJSONArray(ApiConstants.ITEMEXTRAS);
						JSONObject itemextra = itemextras.getJSONObject(0);
						ItemExtra updatedItem = new ItemExtra();
						try {
							updatedItem.setModified(DateUtil.strToDate(itemextra
									.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						updatedItem.setEid(itemextra.getString(ApiConstants.EID));
						itemExtraUpdateResponse.setItemextra(updatedItem);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemExtraUpdateResponse;
    }

	
	protected Map<String, Object> prepareTradeAdd(
			TradeAddRequest request) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.SELLER_NICK, request.getSellerNick());
		params.put(ApiConstants.IIDS, request.getIids());
		params.put(ApiConstants.SKU_IDS, request.getSkuIds());
		params.put(ApiConstants.PRICES, request.getPrices());
		params.put(ApiConstants.NUMS, request.getNums());
		params.put(ApiConstants.TITLES, request.getTitles());
		params.put(ApiConstants.SNAPSHOT, request.getSnapshot());
		
		params.put(ApiConstants.PAY_TITLES, request.getPayTitle());
		params.put(ApiConstants.DISCOUNT_FEES, request.getDiscountFees());
		params.put(ApiConstants.SAVE_DELIVER_ADDR, request.getSaveDeliverAddr());
		params.put(ApiConstants.ITEM_MEMOS, request.getItemMemos());
		params.put(ApiConstants.TRADE_MEMO, request.getTradeMemo());
		
		if (request.getOrderSnapshots() != null)
			params.putAll(request.getOrderSnapshots());
		
		params.put(ApiConstants.BUYER_MESSAGE, request.getBuyerMessage());
		params.put(ApiConstants.LOCATION_ZIP, request.getLocationZip());
		params.put(ApiConstants.DIVISION_CODE, request.getDivisionCode());
		params.put(ApiConstants.LOGISTIC_TYPE, request.getLogisticType());
		params.put(ApiConstants.POST_FEE, request.getPostFee());
		params.put(ApiConstants.RECEIVER_NAME, request.getReceiverName());
		params.put(ApiConstants.LOCATION_ADDRESS, request.getLocationAddress());
		params.put(ApiConstants.PHONE, request.getPhone());
		params.put(ApiConstants.MOBILE, request.getMobile());
		params.put(ApiConstants.LOCATION_STATE, request.getLocationState());
		params.put(ApiConstants.LOCATION_CITY, request.getLocationCity());
		params.put(ApiConstants.LOCATION_DISTRICT, request.getLocationDistrict());
		
		// start by jeck 新增 2009-05-14
		params.put(ApiConstants.TYPE, request.getType());
		params.put(ApiConstants.SHOP_PROMOTION, request.getShopPromotion());
		
		if (request.getItemMeals() != null)
			params.putAll(request.getItemMeals());
		// end 
		
		params.put(ApiConstants.LOCATION_ID, request.getLocationID());
		return params;
	}
	
	protected TradeAddResponse sendTradeAdd(Map<String, Object> params, String sessionId) throws TaobaoApiException{
		TaobaoResponse response = this.getFetch().fetch(this.getServiceUrl(),
				getTemplateRequest(TaobaoDirectApiMethod.TRADE_ADD.getMethod(), sessionId, params));
		TradeAddResponse tradeAddResponse = new TradeAddResponse(response);

		// 平台成功访问
		if (tradeAddResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(tradeAddResponse.getBody());
				parseError(tradeAddResponse, json);
				// 访问淘宝成功
				if (tradeAddResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TRADES)) {
						JSONArray trades = rsp.getJSONArray(ApiConstants.TRADES);
						JSONObject trade = trades.getJSONObject(0);
						Trade addedTrade = new Trade();
						try {
							addedTrade.setCreated(DateUtil.strToDate(trade
									.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						addedTrade.setTid(trade.getString(ApiConstants.TID));
						tradeAddResponse.setTrade(addedTrade);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return tradeAddResponse;
	}
	public TradeAddResponse tradeAdd(TradeAddRequest tradeAddRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeAdd(tradeAddRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradeAdd(params, sessionId);
	}

	protected Map<String, Object> prepareShippingAdd(
			ShippingAddRequest shippingAddRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		return params;
	}
	
	protected ShippingAddResponse sendShippingAdd(Map<String, Object> params, String sessionId) throws TaobaoApiException{
		TaobaoResponse response = this.getFetch().fetch(this.getServiceUrl(),
				getTemplateRequest(TaobaoDirectApiMethod.SHIPPING_ADD.getMethod(), sessionId, params));
		ShippingAddResponse shippingAddResponse = new ShippingAddResponse(response);

		// 平台成功访问
		if (shippingAddResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(shippingAddResponse.getBody());
				parseError(shippingAddResponse, json);
				// 访问淘宝成功
				if (shippingAddResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SHIPPINGS)) {
						JSONArray ships = rsp.getJSONArray(ApiConstants.TRADES);
						JSONObject ship = ships.getJSONObject(0);
						Shipping addShipping = new Shipping();
						try {
							addShipping.setCreated(DateUtil.strToDate(ship
									.getString(ApiConstants.CREATED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
						addShipping.setOutSid(ship.getString(ApiConstants.OUT_SID));
						shippingAddResponse.setShipping(addShipping);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return shippingAddResponse;
	}

	public ShippingAddResponse shippingAdd(ShippingAddRequest shippingAddRequest, String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareShippingAdd(shippingAddRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendShippingAdd(params, sessionId);
	}

	public PostageAddResponse postageAdd(PostageAddRequest request,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = preparePostageAdd(request);
		params.put(SESSION_NICK, sessionNick);
		return sendPostageAdd(params, sessionId);
	}

	public PostageUpdateResponse postageUpdate(PostageUpdateRequest request,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = preparePostageUpdate(request);
		params.put(SESSION_NICK, sessionNick);
		return sendPostageUpdate(params, sessionId);
	}
	
	public PostagesGetResponse postagesGet(PostagesGetRequest request,String sessionId,String sessionNick) throws TaobaoApiException{
		Map<String, Object> params = preparePostageGet(request);
		params.put(SESSION_NICK, sessionNick);
		return sendPostagesGet(params, sessionId);
	}
	
	public PostageDeleteResponse postageDelete(PostageDeleteRequest request,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = preparePostageDelete(request);
		params.put(SESSION_NICK, sessionNick);
		return sendPostageDelete(params, sessionId);
	}

	public ItemUpdateShowcaseResponse itemUpdateShowcase(ItemUpdateShowcaseRequest req,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemRecommendAdd(req);
		params.put(SESSION_NICK, sessionNick);
		return new ItemUpdateShowcaseResponse(addRecommendItem(params, sessionId));
	}

	public ItemUpdateRevokeShowcaseResponse itemUpdateRevokeShowcase(
			ItemUpdateRevokeShowcaseRequest req, String sessionId, String sessionNick)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemRecommendDelete(req);
		params.put(SESSION_NICK, sessionNick);
		return new ItemUpdateRevokeShowcaseResponse(deleteRecommendItem(params, sessionId));
	}

	/**
	 * 此接口方法用以一口价商品的上架
	 * 
	 * @param itemUpdateListingRequest
	 * @return ItemUpdateListingResponse
	 * @throws TaobaoApiException
	 */
	public ItemUpdateListingResponse itemUpdateListing(
			ItemUpdateListingRequest itemUpdateListingRequest, String sessionId,String sessionNick)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemListing(itemUpdateListingRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemUpdateListing(params, sessionId);
	}

	protected Map<String, Object> prepareItemListing(
			ItemUpdateListingRequest itemUpdateListingRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemUpdateListing请求中的参数添加到params
		params.put(ApiConstants.IID, itemUpdateListingRequest.getIid());
		params.put(ApiConstants.NUM, itemUpdateListingRequest.getNum());
		return params;
	}

	protected ItemUpdateListingResponse sendItemUpdateListing(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_UPDATE_LISTING
						.getMethod(), sessionId, params));
		ItemUpdateListingResponse itemUpdateListingResponse = new ItemUpdateListingResponse(
				response);
		// 平台成功访问
		if (itemUpdateListingResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(itemUpdateListingResponse
						.getBody());
				parseError(itemUpdateListingResponse, json);
				// 访问淘宝成功
				if (itemUpdateListingResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						itemUpdateListingResponse.setIid(item
								.getString(ApiConstants.IID));
						try {
							itemUpdateListingResponse.setModified(DateUtil
									.strToDate(item
											.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemUpdateListingResponse;
	}
	/**
	 * 此接口用于商品下架
	 * 
	 * @param itemUpdateDelistingRequest
	 * @return ItemUpdateDelistingResponse
	 * @throws TaobaoApiException
	 */
	public ItemUpdateDelistingResponse itemUpdateDelisting(
			ItemUpdateDelistingRequest itemUpdateDelistingRequest,
			String sessionId,String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemDelisting(itemUpdateDelistingRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemUpdateDelisting(params, sessionId);
	}

	protected Map<String, Object> prepareItemDelisting(
			ItemUpdateDelistingRequest itemUpdateDelistingRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		// 先将itemUpdateDelisting请求中的参数添加到params
		params.put(ApiConstants.IID, itemUpdateDelistingRequest.getIid());
		return params;
	}

	protected ItemUpdateDelistingResponse sendItemUpdateDelisting(
			Map<String, Object> params, String sessionId)
			throws TaobaoApiException {
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_UPDATE_DELISTING
						.getMethod(), sessionId, params));
		ItemUpdateDelistingResponse itemUpdateDelistingResponse = new ItemUpdateDelistingResponse(
				response);
		// 平台成功访问
		if (itemUpdateDelistingResponse.isSuccess()) {
			try {
				JSONObject json = new JSONObject(itemUpdateDelistingResponse
						.getBody());
				parseError(itemUpdateDelistingResponse, json);
				// 访问淘宝成功
				if (itemUpdateDelistingResponse.isSuccess()) {
					// 对返回结果进行处理
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						JSONObject item = items.getJSONObject(0);
						itemUpdateDelistingResponse.setIid(item
								.getString(ApiConstants.IID));
						try {
							itemUpdateDelistingResponse.setModified(DateUtil
									.strToDate(item
											.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							throw new TaobaoApiException(e);
						}
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemUpdateDelistingResponse;
	}
	/**
	 * 更新库存信息
	 * 
	 * @param skuUpdateRequest
	 * @return SkuUpdateResponse
	 * @throws TaobaoApiException
	 */
	public SkuUpdateResponse skuUpdate(SkuUpdateRequest skuUpdateRequest,
			String sessionId,String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareSkuUpdate(skuUpdateRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendSkuUpdate(params, sessionId);
	}

	protected Map<String, Object> prepareSkuUpdate(
			SkuUpdateRequest skuUpdateRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.IID, skuUpdateRequest.getIid());
		params.put(ApiConstants.PROPERTIES, skuUpdateRequest.getProperties());
		params.put(ApiConstants.QUANTITY, skuUpdateRequest.getQuantity());
		params.put(ApiConstants.PRICE, skuUpdateRequest.getPrice());
		params.put(ApiConstants.OUTER_ID, skuUpdateRequest.getOuterId());
		params.put(ApiConstants.LANG, skuUpdateRequest.getLang());
		return params;
	}

	protected SkuUpdateResponse sendSkuUpdate(Map<String, Object> params,
			String sessionId) throws TaobaoApiException {
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_SKU_UPDATE.getMethod(),
						sessionId, params));
		SkuUpdateResponse skuUpdateResponse = new SkuUpdateResponse(response);
		if (skuUpdateResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(skuUpdateResponse.getBody());
				parseError(skuUpdateResponse, json);
				if (skuUpdateResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.SKUS)) {
						JSONArray skus = rsp.getJSONArray(ApiConstants.SKUS);
						JSONObject sku = skus.getJSONObject(0);
						Sku updatedSku = new Sku();
						updatedSku.setIid(sku.getString(ApiConstants.IID));
						try {
							updatedSku.setModified(DateUtil.strToDate(sku
									.getString(ApiConstants.MODIFIED)));
						} catch (ParseException e) {
							e.printStackTrace();
						}
						skuUpdateResponse.setSku(updatedSku);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return skuUpdateResponse;
	}

	public TradeCloseResponse tradeClose(TradeCloseRequest tradeCloseRequest,
			String sesssionID, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeClose(tradeCloseRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendTradeClose(params, sesssionID);
	}

	public SellerAuthorizeGetResponse sellerAuthorizeGet(SellerAuthorizeGetRequest req,
			String sessionId, String sessionNick) throws TaobaoApiException {
		return new SellerAuthorizeGetResponse(itemCatsAuthorizeGet(req, sessionId, sessionNick));
	}

	public ItemCatsAuthorizeGetResponse itemCatsAuthorizeGet(ItemCatsAuthorizeGetRequest req,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemCatsAuthorizeGet(req);
		params.put(SESSION_NICK, sessionNick);
		return getAuthorizeItemCats(params, sessionId);
	}

	public TradeGetResponse tradeNbssnapGet(TradeGetRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		return tradeSnapshotGet(req, sessionId, sessionNick);
	}

	public ItemJointImgResponse itemJointImg(ItemJointImgRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.ITEMIMG_ID, req.getItemImgId());
		params.put(ApiConstants.IID, req.getIid());
		params.put(ApiConstants.POSITION, req.getPosition());
		params.put(ApiConstants.ISMAJOR, req.isMajor());
		params.put(ApiConstants.URL, req.getUrl());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse taobaoResponse = this.getFetch().fetch(this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEM_JOINT_IMG.getMethod(), sessionId, params));
		ItemJointImgResponse itemJointImgResponse = new ItemJointImgResponse(taobaoResponse);
		if (itemJointImgResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemJointImgResponse.getBody());
				parseError(itemJointImgResponse, json);
				if (itemJointImgResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMIMGS)) {
						JSONArray itemImgs = rsp
								.getJSONArray(ApiConstants.ITEMIMGS);
						JSONObject itemImg = itemImgs.getJSONObject(0);
						ItemImg uploadItemImg = new ItemImg();
						uploadItemImg.setItemimgId(itemImg
								.getString(ApiConstants.ITEMIMG_ID));
						uploadItemImg.setUrl(itemImg
								.getString(ApiConstants.URL));
						uploadItemImg.setCreated(itemImg
								.getString(ApiConstants.CREATED));
						itemJointImgResponse.setItemImg(uploadItemImg);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemJointImgResponse;
	}
	public ItemJointPropimgResponse itemJointPropimg(
			ItemJointPropimgRequest itemJointPropimgRequest, String sessionId,String sessionNick)
			throws TaobaoApiException{
		Map<String, Object> params = new HashMap<String, Object>();
		params
				.put(ApiConstants.PROPIMG_ID, itemJointPropimgRequest
						.getPropImgId());
		params.put(ApiConstants.IID, itemJointPropimgRequest.getIid());
		params.put(ApiConstants.PROPERTIES, itemJointPropimgRequest
				.getProperties());
		params.put(ApiConstants.POSITION, itemJointPropimgRequest.getPosition());
		params.put(ApiConstants.URL, itemJointPropimgRequest.getUrl());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse taobaoResponse= this.getFetch().fetch(
					this.getServiceUrl(),
					getTemplateRequest(TaobaoApiMethod.ITEM_JOINT_PROPIMG
							.getMethod(), sessionId, params));
		ItemJointPropimgResponse  itemJointPropimgResponse =new ItemJointPropimgResponse(taobaoResponse);
		if (itemJointPropimgResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemJointPropimgResponse.getBody());
				parseError(itemJointPropimgResponse, json);
				if (itemJointPropimgResponse.isSuccess()) {
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.PROPIMGS)) {
						JSONArray propimgs = rsp
								.getJSONArray(ApiConstants.PROPIMGS);
						JSONObject propimg = propimgs.getJSONObject(0);
						PropImg uploadPropImg = new PropImg();
						uploadPropImg.setPropimg_id(propimg
								.getString(ApiConstants.PROPIMG_ID));
						uploadPropImg.setUrl(propimg
								.getString(ApiConstants.URL));
						uploadPropImg.setCreated(propimg
								.getString(ApiConstants.CREATED));
						itemJointPropimgResponse.setPropImg(uploadPropImg);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemJointPropimgResponse;
		
	}

	public VasSuitesgGetResponse vasSuitesGet(VasSuitesgGetRequest vasSuitesgGetRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareVasSuitesGet(vasSuitesgGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendVasSuitesGet(params, sessionId);
	}

	public VasProductGetResponse vasProductGet(
			VasProductGetRequest vasProductGetRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareVasProductGet(vasProductGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return getVasProduct(params, sessionId);
	}


	public FullitemsGetResponse fullitems(
			FullitemsGetRequest fullitemsGetRequest, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("outer_id", fullitemsGetRequest.getOuterId());
		params.put("fields", fullitemsGetRequest.getFields());
		params.put(SESSION_NICK, sessionNick);
		TaobaoResponse response = this.getFetch().fetch(
				this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.ITEMS_CUSTOM_GET.getMethod(),
						sessionId, params));
		FullitemsGetResponse itemsOutGetResponse = new FullitemsGetResponse(
				response);
		if (itemsOutGetResponse.isSuccess()) {
			JSONObject json;
			try {
				json = new JSONObject(itemsOutGetResponse.getBody());
				parseError(itemsOutGetResponse, json);
				if (itemsOutGetResponse.isSuccess()) {
					// 处理返回结果
					JSONObject rsp = json.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.ITEMS)) {
						JSONArray items = rsp.getJSONArray(ApiConstants.ITEMS);
						itemsOutGetResponse.setItem(TaobaoItemJSONConvert
								.convertJsonArrayToItemList(items));
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}
		return itemsOutGetResponse;
	}


	public GuarantyFundsGetResponse guarantyFundsGet(
			GuarantyFundsGetRequest request, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.BEGIN_DATE, request.getBeginDate());
		params.put(ApiConstants.END_DATE, request.getEndDate());
		params.put(SESSION_NICK, sessionNick);
		return getGuarantyFunds(params, sessionId);
	}

	public SubappApplyResponse subappApply(SubappApplyRequest request)
			throws TaobaoApiException {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(ApiConstants.PARENT_APP_KEY, request.getParentAppKey());
		params.put(ApiConstants.TITLE, request.getTitle());
		params.put(ApiConstants.CALLBACK_URL, request.getCallbackUrl());
		params.put(ApiConstants.NOTIFY_URL, request.getNotifyUrl());
		return applySubapp(params);
	}

	protected SubappApplyResponse applySubapp(Map<String, Object> params)
			throws TaobaoApiException {
		TaobaoResponse response = getFetch().fetch(this.getServiceUrl(),
				getTemplateRequest(TaobaoApiMethod.SUBAPP_APPLY.getMethod(), params));
		SubappApplyResponse saResponse = new SubappApplyResponse(response);

		if (saResponse.isSuccess()) {
			try {
				JSONObject jsonBody = new JSONObject(saResponse.getBody());
				parseError(saResponse, jsonBody);
				if (saResponse.isSuccess()) {
					JSONObject rsp = jsonBody.getJSONObject(ApiConstants.RSP);
					if (rsp.has(ApiConstants.TADGET)) {
						JSONObject json = rsp.getJSONObject(ApiConstants.TADGET);
						Tadget tadget = TaobaoAppJSONConvert.convertJsonToTadget(json);
						saResponse.setSubapp(tadget);
					}
				}
			} catch (JSONException e) {
				throw new TaobaoApiException(e);
			}
		}

		return saResponse;
	}
	
	public ItemVideoDeleteResponse itemVideoDelete(
			ItemVideoDeleteRequest itemVideoDeleteRequest, String sessionId, String sessionNick)
			throws TaobaoApiException {
		Map<String, Object> params = prepareItemVideoDelete(itemVideoDeleteRequest);
		params.put(SESSION_NICK, sessionNick);
		return sendItemVideoDelete(params, sessionId);
	}

	public ItemVideoUploadResponse itemVideoUpload(ItemVideoUploadRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemVideoUpload(req);
		params.put(SESSION_NICK, sessionNick);
		return sendItemVideoUpload(params, sessionId);
	}

	public SkusCustomGetResponse skusCustomGet(SkusCustomGetRequest skusCustomGetRequest,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareSkusCustomGet(skusCustomGetRequest);
		params.put(SESSION_NICK, sessionNick);
		return getCustomSkus(params, sessionId);
	}

	public ItemRecommendDeleteResponse itemRecommendDelete(ItemRecommendDeleteRequest req,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemRecommendDelete(req);
		params.put(SESSION_NICK, sessionNick);
		return deleteRecommendItem(params, sessionId);
	}

	public ItemRecommendAddResponse itemRecommendAdd(ItemRecommendAddRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareItemRecommendAdd(req);
		params.put(SESSION_NICK, sessionNick);
		return addRecommendItem(params, sessionId);
	}

	public TradeGetResponse tradeSnapshotGet(TradeGetRequest req, String sessionId,
			String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareTradeGet(req);
		params.put(SESSION_NICK, sessionNick);
		return getTradeSnapshot(params, sessionId);
	}

	public LogisticsOrdersDetailGetResponse logisticsOrdersDetailGet(
			LogisticsOrdersDetailGetRequest req, String sessionId, String sessionNick)
			throws TaobaoApiException {
		Map<String, Object> params = prepareLogisticsOrderGet(req);
		params.put(SESSION_NICK, sessionNick);
		return getLogisticsOrdersDetail(params, sessionId);
	}

	public LogisticsOrdersGetResponse logisticsOrdersGet(LogisticsOrdersGetRequest request,
			String sessionId, String sessionNick) throws TaobaoApiException {
		Map<String, Object> params = prepareLogisticsOrderGet(request);
		params.put(SESSION_NICK, sessionNick);
		return getLogisticsOrders(params, sessionId);
	}
}
